package com.eax.CLILayer;
import com.eax.ControlLayer.DVDController;
import com.eax.ControlLayer.*;
import com.eax.ModelLayer.*;
import java.util.ArrayList;
import java.util.Scanner;

public class LendUI {
	private LendController lendController;
	private DVDController dvdController;
	private AddressController addressController;
	private ReservationController reservationController;

	public LendUI(LendController lendController, DVDController dvdController, AddressController addressController, ReservationController reservationController) {
		this.lendController = lendController;
		this.dvdController = dvdController;
		this.addressController = addressController;
		this.reservationController = reservationController;
	}

	public void LendMenu() {
		boolean exit = false;

		while(!exit) {
			//show menu and return the user's choice
			int choice = writeLendMenu();

			switch (choice) {
				case 1:
					findLend();
					break;
				case 2:
					createLend();
					break;
				case 3:
					returnLend();
					break;
				case 4:
					listAllLends();
					break;
				default:
					exit = true;
					break;
			}
		}
	}

	private int writeLendMenu()  {
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.println("\f *** Lend Menu ***");
		System.out.println(" (1) Find a loan");
		System.out.println(" (2) Create loan");
		System.out.println(" (3) Return a loaned item");
		System.out.println(" (4) Show all loans");
		System.out.println(" (6) Return");
		System.out.print("\n Make your choice: ");

		return keyboard.nextInt();
	}

	private void findLend() {
		int id = inputId();
		LendModel x = lendController.findLendById(id);
		x.printInfo();
	}

	private void createLend(){
		int id = lendController.getHighestId() + 1;
		int copySerialNumber = inputSerialNumber();
		int personId = inputPerson();
		String startDate = inputStartDate();
		String returnDate = "";
		String lendingPeriodStart = startDate;
		String lendingPeriodReturn = inputLendingPeriodReturn();
		String status = "lend";

		lendController.addLend(id, copySerialNumber, personId, startDate, returnDate, lendingPeriodStart, lendingPeriodReturn, status);
		dvdController.rentCopy(copySerialNumber);
	}

	private void returnLend(){
		int id = inputId();
		Boolean ok = false;
		LendModel lend = lendController.findLendById(id);
		CopyModel copy = dvdController.getCopyBySerialNumber(lend.getCopySerialNumber());
		DVDModel dvd = dvdController.findDVD(copy.getDvdId());

		lendController.returnLend(id); // We have returned the lend.
		dvdController.returnCopy(lend.getCopySerialNumber()); // We have returned the copy.
		// Let's check whether or not there's a reservation for this DVD!
		for(ReservationModel r : reservationController.getAllReservations()){
			if(r.getDVDID() == dvd.getId() && !ok){
				r.setStatus("Ready to pick up!");
				ok = true;
			}
		}
	}

	private void listAllLends(){
		for(LendModel l : lendController.getAllLends()){
			l.printInfo();
		}
	}

	private int inputId(){
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type id: ");
		return keyboard.nextInt();
	}

	private int inputPerson(){
		Boolean ok = false;
		PersonModel person = null;
		String input;
		int id = 0;

		while(!ok){
			Scanner keyboard = new Scanner(System.in);
			System.out.print("Find person - Type search input: ");
			input = keyboard.nextLine();

			person = addressController.findPerson(input);

			if(person != null){
				id = person.getId();
				ok = true;
			} else {
				System.out.println("Person not found. Try again!");
			}
		}

		return id;
	}

	private int inputSerialNumber(){
		int copySerialNumber = 0;
		int choice;
		Boolean ok = false;
		ArrayList<DVDModel> dvds = dvdController.getAllDvds();

		while(!ok){
			System.out.println("Please select a DVD to create a lend copy for: ");
			for(DVDModel d : dvds){
				System.out.println(d.getId() + ") "+d.getTitle());
			}

			Scanner keyboard = new Scanner(System.in);
			System.out.print("Selection: ");
			choice = keyboard.nextInt();

			DVDModel dvd = dvdController.findDVD(choice);
			int hasAvailable = dvdController.getAvailableCopy(choice);
			if(dvd != null && hasAvailable != 0){
				ok = true;
				copySerialNumber = choice;
			} else if(hasAvailable == 0){
				System.out.println("No available copies. Please select another DVD!");
			}
		}

		return copySerialNumber;
	}

	private String inputStartDate(){
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type start date of the lend: ");
		return keyboard.nextLine();
	}

	private String inputLendingPeriodReturn(){
		// creates an object keyboard to read data from the keyboard
		Scanner keyboard = new Scanner(System.in);
		System.out.print("Type the date it is supposed to be returned: ");
		return keyboard.nextLine();
	}
}